/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root 注意这题的右侧指的是每层最右
 * @return {number[]}
 */
 var rightSideView = function(root) {
    let res = []
    let queue = []
    queue.unshift(root)
    if(root==null){
        return []
    }
    while(queue.length!==0){        
        let len = queue.length;      
       
        while(len--){
            let node = queue.pop();
            node.left&&queue.unshift(node.left)
            node.right&&queue.unshift(node.right)
            if(len==0){
                res.push(node.val)
            }
        }       
        
    }
    return res
};